package com.leon.learnenginespace.drools.utils.calculate.impl;

import com.leon.learnenginespace.drools.utils.FieldException;
import com.leon.learnenginespace.drools.utils.calculate.ICalculateBases;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 加法
 */
@Component
public class CalculateAdd implements ICalculateBases {
    /**
     * v1+v2
     *
     * @param v1
     * @param v2
     * @param dataType
     * @return
     */
    @Override
    public Object cal(Object v1, Object v2, String dataType) {
        if (StringUtils.isEmpty(dataType)) {
            throw new FieldException("数据类型为空");
        }
        dataType = dataType.toUpperCase();
        String v1Str = (String) v1;
        String v2Str = (String) v2;
        if ("NUMBER".equals(dataType)) {
            BigDecimal bv1 = new BigDecimal(v1Str);
            BigDecimal bv2 = new BigDecimal(v2Str);
            return bv1.add(bv2);
        } else {
            throw new FieldException("加法只技术NUMBER类型数据");
        }
    }
}
